package com.starbaba.spread.view;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.controllog.aop.LogAble;
import com.starbaba.controllog.model.ControlLogCategory;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.spread.model.ChannelVO;
import com.starbaba.spread.model.SpreadChannelModel;
import com.starbaba.spread.model.getChannelModel;
import com.starbaba.spread.service.channelService;
import com.starbaba.tools.sequence.Sequence;

public class channelListAction extends BaseAction implements ModelDriven {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1629639856220872834L;

	private getChannelModel model;

	@Resource(name = "channelServiceImpl")
	private channelService service;

	private InputStream input;

	private String fileName;

	private ArrayList<ControlLogCategory> logCategorysArrayList;

	public channelListAction() {
		logCategorysArrayList = new ArrayList<ControlLogCategory>();
	}

	public String getFileName() {
		return fileName;
	}

	public String showPage() {
		return "success";
	}

	public void channelListRequest() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String channel = (String) paramsMap.get("channel");
		String parentid = (String) paramsMap.get("parentid");
		// getMainList------搜索主栏目下status为1的全部子栏目
		// getChildrenList--搜索主栏目下全部子栏目
		// getAllList--搜索全部栏目(渠道列表用)
		String getMainList = (String) paramsMap.get("getMainList");
		String getChildrenList = (String) paramsMap.get("getChildrenList");
		String getAllList = (String) paramsMap.get("getAllList");
		if (null != channel) {
			model.setChannel(channel);
		}
		String status = (String) paramsMap.get("status");
		if (null != status) {
			model.setStatus(Integer.valueOf(status));
		}
		if (null != getMainList) {
			model.setParentid(Long.valueOf(parentid));
			model.setStatus(1);
		}
		if (null != getAllList) {
			model.setGetAllList(getAllList);
		}

		parsePageRequest();
		try {
			PageResponse pageResponse = new PageResponse();
			Integer startPoint = pageRequest.getIDisplayStart();
			Integer displayLength = pageRequest.getIDisplayLength();
			if (null != getMainList || null != getChildrenList) {
				startPoint = 0;
				displayLength = 100000;
			}
			List list = service.getDataList(startPoint, displayLength, model, pageResponse, getService());
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			String jsonStr = JsonUtils.toJson(pageResponse, null, true, null, false);
			sendJsonMessage(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 用id查询本条记录
	public void channelshowwz() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String parentid = (String) paramsMap.get("parentid");
		try {
			PageResponse pageResponse = new PageResponse();
			List list = this.getService().doFindByProperty(ChannelVO.class, "id", Long.valueOf(parentid));
			String jsonStr = JsonUtils.toJson(list, null, true, null, false);
			sendJsonMessage(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@LogAble(operate = LogAble.OPERATE_ADD)
	public void addChannel() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String channel = (String) paramsMap.get("channel");
		String channelParentid = (String) paramsMap.get("channelParentid");
		String showwz = (String) paramsMap.get("showwz");
		Long id = Sequence.getSequence();
		try {
			ChannelVO entity = new ChannelVO();
			entity.setName(String.valueOf(channel));
			entity.setMemo(String.valueOf(channel));
			entity.setId(id);
			entity.setStatus(1);
			entity.setShowwz(Integer.valueOf(showwz));
			entity.setParentid(Long.valueOf(channelParentid));

			Object ret = this.getService().doCreate(ChannelVO.class, entity);
			if (null == ret) {
				sendErrMsg("新增失败");
			} else {
				JsonObject result = new JsonObject();
				ResultJson resultJs = new ResultJson();
				resultJs.msg = "添加成功";
				result.add("result", resultJs.toJSON());
				SpreadChannelModel spreadChanel = new SpreadChannelModel();
				spreadChanel.getChannelList();
				sendJsonMessage(result.toString());
				ControlLogCategory logCategory = new ControlLogCategory();
				logCategory.setId(String.valueOf(id));
				logCategory.setName(String.valueOf(channel));
				logCategorysArrayList.add(logCategory);
			}
		} catch (Exception e) {
			sendErrMsg(e.getMessage());
		}
	}

	// 上下线 online or offline
	@LogAble(operate = LogAble.OPERATE_MODIFY)
	public void oLine() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		// 操作类别：0为全部订单，1未完成订单，2为成功订单，3为失败订单
		String id = (String) paramsMap.get("id");
		String type = (String) paramsMap.get("type");
		try {
			ChannelVO entity = new ChannelVO();
			List<?> list = this.getService().doFindByProperty(ChannelVO.class, "id", Long.valueOf(id));
			if (null != list && list.size() > 0) {
				entity.setId(Long.valueOf(id));
				Object ret = this.getService().executeSQLUpdate(
						"UPDATE loan.spread_channel SET status = '" + Integer.valueOf(type) + "' WHERE id = '"
								+ Long.valueOf(id) + "' ");
				if (null == ret) {
					sendErrMsg("修改失败");
				} else {
					JsonObject result = new JsonObject();
					ResultJson resultJs = new ResultJson();
					resultJs.msg = "修改成功";
					result.add("result", resultJs.toJSON());
					SpreadChannelModel spreadChanel = new SpreadChannelModel();
					spreadChanel.getChannelList();
					sendJsonMessage(result.toString());
					ControlLogCategory logCategory = new ControlLogCategory();
					logCategory.setId(id);
					logCategory.setName("");
					logCategorysArrayList.add(logCategory);
				}
			}
		} catch (Exception e) {
			sendErrMsg(e.getMessage());
		}
	}

	// 修改渠道
	@LogAble(operate = LogAble.OPERATE_MODIFY)
	public void changeProperty() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		// 操作类别：0为全部订单，1未完成订单，2为成功订单，3为失败订单
		String id = (String) paramsMap.get("id");
		String name = (String) paramsMap.get("name");
		String parentid = (String) paramsMap.get("parentid");
		String showwz = (String) paramsMap.get("showwz");
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE loan.spread_channel SET");
		if (null != name && null != parentid) {
			sql.append(" name = '" + name + "', memo = '" + name + "', parentid = " + Long.valueOf(parentid));
		} else {
			if (null != name) {
				sql.append(" name = '" + name + "', memo = '" + name + "'");
			}
			if (null != parentid) {
				sql.append(" parentid = " + Long.valueOf(parentid));
			}
			if (null != showwz) {
				sql.append(" ,showwz = " + Integer.valueOf(showwz));
			}
		}
		sql.append(" WHERE id = " + Long.valueOf(id));
		try {
			ChannelVO entity = new ChannelVO();
			List<?> list = this.getService().doFindByProperty(ChannelVO.class, "id", Long.valueOf(id));
			if (null != list && list.size() > 0) {
				entity.setId(Long.valueOf(id));
				Object ret = this.getService().executeSQLUpdate(String.valueOf(sql));
				if (null == ret) {
					sendErrMsg("修改失败");
				} else {
					JsonObject result = new JsonObject();
					ResultJson resultJs = new ResultJson();
					resultJs.msg = "修改成功";
					result.add("result", resultJs.toJSON());
					SpreadChannelModel spreadChanel = new SpreadChannelModel();
					spreadChanel.getChannelList();
					sendJsonMessage(result.toString());
					ControlLogCategory logCategory = new ControlLogCategory();
					logCategory.setId(id);
					logCategory.setName(name);
					logCategorysArrayList.add(logCategory);
				}
			}
		} catch (Exception e) {
			sendErrMsg(e.getMessage());
		}
	}

	@Override
	public Object getModel() {
		if (model == null) {
			model = new getChannelModel();
		}
		return model;
	}

	private void sendErrMsg(String msg) {
		JsonObject result = new JsonObject();
		ResultJson resultJs = new ResultJson();
		resultJs.msg = msg;
		result.add("result", resultJs.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getActionId() {
		return "001301";
	}

	@Override
	public ArrayList<ControlLogCategory> getProduct() {
		return logCategorysArrayList;
	}

}
